﻿Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports IETracker.Business.Entities
Imports IETracker.Data

Public Class TransactionListComponent

    Public Function GenerateTransactionList(v_transactionType As String, Optional endDate As Date = #12:00:00 AM#, Optional returnNumberOfTransactions As Integer = 0) As List(Of TransactionList)

        Dim transactionList As New List(Of TransactionList)
        Dim dac As New IETrackerDataAccess
        Try
            transactionList = If(returnNumberOfTransactions = 0 _
                                 , dac.GetAllTransactions(v_transactionType).Where(Function(x) x.TransactionDate >= endDate) _
                                 , dac.GetAllTransactions(v_transactionType).Take(returnNumberOfTransactions)).ToList
        Catch ex As Exception
            Throw ex
        End Try
        Return transactionList
    End Function

    Public Function GetTransactionTypes(v_transactionType As String) As List(Of String)
        Dim dac As New IETrackerDataAccess
        Dim _transactionTypes As List(Of String) = Nothing
        Try
            Dim transTypes = dac.GetAllTransactionDescription(v_transactionType)
            _transactionTypes = transTypes
        Catch ex As Exception
            Throw ex
        End Try

        Return _transactionTypes
    End Function


    Public Function ReadAllTransactions() As List(Of Transaction)
        Dim dac As New IETrackerDataAccess
        Dim _transactionTypes As List(Of Transaction) = Nothing
        Try
            Dim transTypes = dac.ReadAllTransactions()
            _transactionTypes = transTypes
        Catch ex As Exception
            Throw ex
        End Try

        Return _transactionTypes
    End Function

    Public Function CreateTransaction(_transactionList As TransactionList) As TransactionList
        Dim dac As New IETrackerDataAccess

        Try
            Dim transactionTypeId As Integer

            'income or expense
            If _transactionList.TransactionTypeDesc.ToUpper = "EXPENSE" Then
                transactionTypeId = 2
            ElseIf _transactionList.TransactionTypeDesc.ToUpper = "INCOME" Then
                transactionTypeId = 1
            Else
                Throw New Exception("Invalid transaction type.")
            End If

            'transaction description - petrol, pay, grocery, etc.
            Dim _transactionType As New TransactionType
            Dim _transactionDescription As TransactionDescription = dac.ReadTransactionDescription(_transactionList.TransactionDescription)
            If _transactionDescription Is Nothing Then
                _transactionDescription = New TransactionDescription
                _transactionDescription.TransactionDescription1 = _transactionList.TransactionDescription
                _transactionDescription = dac.CreateTransctionDescription(_transactionDescription)

                'create transaction type
                _transactionType.TransactionTypeDescID = transactionTypeId
                _transactionType.TransactionDescID = _transactionDescription.TransactionDescriptionID
                _transactionType = dac.CreateTransactionType(_transactionType)
            Else
                _transactionType = dac.ReadTransactionType(_transactionDescription.TransactionDescriptionID)
            End If

            Dim _transaction As New Transaction
            _transaction.TransactionDate = _transactionList.TransactionDate
            _transaction.TransactionAmount = _transactionList.TransactionAmount
            _transaction.TransactionTypeID = _transactionType.TransactionTypeID
            _transaction.Comment = _transactionList.TransactionComment
            _transaction.TransactionID = _transactionList.TransactionID
            If _transactionList.TransactionID = Nothing Then
                'create transaction                
                _transactionList.TransactionID = dac.CreateTransaction(_transaction).TransactionID
            Else
                'edit transaction                
                dac.UpdateTransaction(_transaction)
            End If
            Return _transactionList
        Catch ex As Exception
            Throw ex
        End Try

        
    End Function

End Class
